//
//  ViewController.m
//  20505
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,assign) float cheese;
@property(nonatomic,assign) float cheese1;
@property(nonatomic,assign) float cheese2;
@property(nonatomic,assign) float cheese3;
@property(nonatomic,strong) NSLock *lock;
@end

@implementation ViewController
// 练习:
// 一个盒子里有100块奶酪,有个鼠洞通到盒子
// 3只老鼠,老鼠需要把奶酪叼回自己的窝里，每只老鼠搬得速度不同。(起始的时候，每只老鼠窝里有10块奶酪)
// 鼠洞很小，每一刻只能有一只老鼠在里面通行。（搬奶酪过程，线程互斥）
// 每只老鼠跑动速度不同(来回搬一次时间为3s 4s 5s)
// 老鼠如果不在叼奶酪，就是在窝里吃奶酪(每秒吃0.1块)，显示最后3只老鼠窝里的奶酪数量，如果没得吃，那只老鼠饿死。
// 线程结束条件:盒子里奶酪没了,或者饿死了。
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.cheese=100;
    self.cheese1=10;
    self.cheese2=10;
    self.cheese3=10;
    self.lock=[[NSLock alloc]init];
    NSThread *mouse1=[[NSThread alloc]initWithTarget:self selector:@selector(movecheese) object:nil];
    mouse1.name=@"老鼠(王源)";
    NSThread *mouse2=[[NSThread alloc]initWithTarget:self selector:@selector(movecheese) object:nil];
    mouse2.name=@"老鼠(王俊凯)";
    NSThread *mouse3=[[NSThread alloc]initWithTarget:self selector:@selector(movecheese) object:nil];
    mouse3.name=@"老鼠(易烊千玺)";
    [mouse1 start];
    [mouse2 start];
    [mouse3 start];
}

-(void)movecheese
{
    while (1)
    {
        if(self.cheese1==0 || self.cheese2==0 || self.cheese3==0)
        {
            [NSThread exit];
        }
        else
        {
        if([self.lock tryLock])
        {
            int count=self.cheese;
            if(count>0)
            {
                if([[NSThread currentThread].name isEqualToString:@"老鼠(王源)"])
                {
                    [NSThread sleepForTimeInterval:0.3];
                    self.cheese1+=1;
                    self.cheese-=1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了奶酪他有:%.1f剩余:%.1f",[NSThread currentThread].name,self.cheese1,self.cheese);
                    NSLog(@"休息一下");
                    [NSThread sleepForTimeInterval:0.2];
                }
                else if([[NSThread currentThread].name isEqualToString:@"老鼠(王俊凯)"])
                {
                    [NSThread sleepForTimeInterval:0.4];
                    self.cheese2+=1;
                    self.cheese-=1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了奶酪他有:%.1f剩余:%.1f",[NSThread currentThread].name,self.cheese2,self.cheese);
                    NSLog(@"休息一下");
                    [NSThread sleepForTimeInterval:0.2];
                }

                else
                {
                    [NSThread sleepForTimeInterval:0.5];
                    self.cheese3+=1;
                    self.cheese-=1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了奶酪他有:%.1f剩余:%.1f",[NSThread currentThread].name,self.cheese3,self.cheese);
                    NSLog(@"休息一下");
                    [NSThread sleepForTimeInterval:0.2];
                }

            }
            else
            {
                NSLog(@"奶酪没有了");
                [self.lock unlock];
                [NSThread exit];
            }
        }
        else
        {
            if([[NSThread currentThread].name isEqualToString:@"老鼠(王源)"])
            {
                [NSThread sleepForTimeInterval:0.1];
                self.cheese1-=0.1;
                NSLog(@"%@:吃奶酪剩余:%.1f",[NSThread currentThread].name,self.cheese1);
            }
            else if([[NSThread currentThread].name isEqualToString:@"老鼠(王俊凯)"])
            {
                [NSThread sleepForTimeInterval:0.1];
                self.cheese2-=0.1;
                NSLog(@"%@:吃奶酪剩余:%.1f",[NSThread currentThread].name,self.cheese2);
            }
            
            else
            {
                [NSThread sleepForTimeInterval:0.1];
                self.cheese3-=0.1;
                NSLog(@"%@:吃奶酪剩余:%.1f",[NSThread currentThread].name,self.cheese3);
            }

        }
        }
    }
}

@end
